package com.panopset.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

import com.panopset.games.HighScores;
import com.panopset.gui.PopupBox;
import com.panopset.gui.TLabel;
import com.panopset.gui.TPanel;
import com.panopset.java.Version;
import com.panopset.swing.FontManager.FontType;

/**
 * High score list for any Java Swing game.
 *
 * @author Karl Dinwiddie
 *
 */
public final class HighScoreList extends TPanel {

    private final HighScores hs;
    
    private static final String TEST_LIST = "com.panopset.swing.HighScoreTest.txt";

    public HighScoreList(final String testList) {
        super(new GridBagLayout());
        hs = new HighScores(testList);
        update();
    }


	/**
	 * @param name Name.
	 * @param value Value.
	 * @return true if this was one of the high scores, false if not.
	 * Useful for a "Congratulations" message.
	 */
	public boolean setScore(final String name, final Long value) {
    	boolean rtn = hs.setScore(name, value);
    	if (rtn) {
        	update();
    	}
    	return rtn;
    }
    
    public void update() {
    	removeAll();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = 0;
        int i = 1;
        for (HighScores.Score score : hs.getScores()) {
            gbc.gridx = 0;
            gbc.weightx = 1.0;
            gbc.anchor = GridBagConstraints.EAST;
            TLabel nbrLbl = new TLabel("" + i); 
            if (i == 1) {
                FontManager.register(nbrLbl, FontType.boldArial);
                nbrLbl.setForeground(Color.BLACK);
            } else {
                FontManager.register(nbrLbl, FontType.plainArial);
                nbrLbl.setForeground(Color.GRAY);
            }
            add(nbrLbl, gbc);
            gbc.gridx++;
            gbc.weightx = 0.1;
            gbc.anchor = GridBagConstraints.CENTER;
            add(new TLabel(" "), gbc);
            gbc.gridx++;
            gbc.weightx = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            TLabel snlbl = new TLabel(score.getName());
            if (i == 1) {
                FontManager.register(snlbl, FontType.boldArial);
                snlbl.setForeground(Color.BLACK);
            } else {
                FontManager.register(snlbl, FontType.plainArial);
                snlbl.setForeground(Color.GRAY);
            }
            add(snlbl, gbc);
            gbc.gridx++;
            gbc.weightx = 0.2;
            gbc.anchor = GridBagConstraints.CENTER;
            add(new TLabel(" "), gbc);
            gbc.gridx++;
            gbc.weightx = 0.0;
            gbc.anchor = GridBagConstraints.EAST;
            TLabel svlbl = new TLabel("" + score.getValue()); 
            if (i == 1) {
                FontManager.register(svlbl, FontType.boldArial);
                svlbl.setForeground(Color.BLACK);
            } else {
                FontManager.register(svlbl, FontType.plainArial);
                svlbl.setForeground(Color.GRAY);
            }
            add(svlbl, gbc);
            gbc.gridx++;
            gbc.weightx = 1.0;
            gbc.anchor = GridBagConstraints.EAST;
            add(new TLabel(""), gbc);
            gbc.gridy++;
            i++;
        }
        invalidate();
        Component p = getParent();
        if (p != null) {
        	p.validate();
        }
    }

    /**
     * @param args
     */
    public static void main(final String[] args) {
        new AppPanopset() {

            @Override
            public Component createMainPanel() {
            	HighScoreList hs = new HighScoreList(TEST_LIST);
            	hs.setScore("Beamr5ASDFA5fgsdfgsdfgsdgd", new Double(Math.random() * Long.MAX_VALUE).longValue());
                return hs;
            }

            @Override
            protected PopupBox getAboutBox() {
                return new PopupBox(new JPanel(), new Dimension(100,100), f.getFrame());
            }

        }.show();
    }


    
    /**
     * 
     */
    private static final long serialVersionUID = Version.UNI_VERSION;
}
